<template>
  <div>
    <van-address-list v-model="chosenAddressId" :list="addressList" default-tag-text="默认" @add="handleAdd"
                      @edit="onEdit" @select="handleSelect"/>
  </div>

</template>

<script>
//获取地址列表
import * as AddressAPI from "@/api/address.js";

export default {
  data() {
    return {
      chosenAddressId: '',
      addressList: [],
    };
  },

  created() {
    this.chosenAddressId = this.$route.params.addressId || "";
    this.getMyList();
  },

  methods: {
    getMyList() {
      AddressAPI.getMyList().then(result => {
        result.data.forEach(item => {
          this.addressList.push({
            id: item.id,
            name: item.name,
            tel: item.tel,
            address: item.city + item.county + item.addressDetail,
            isDefault: item.isDefault == 1 ? true : false,
          });
        });
      })
    },

    // 触发增加
    handleAdd() {
      this.$router.push({
        name: "addressEdit"
      });
    },

    // 触发编辑
    onEdit(item) {
      this.$router.push({
        name: "addressEdit",
        params: {
          id: item.id
        },
      });
    },

    //处理点击选择列表
    handleSelect(item) {
      let addressId = item.id;
      //路由有参数表明是要选择地址id的,把选择结果路由传参返回。
      if (this.$route.params.addressId) {
        this.$router.replace({
          name: "orderCreate",
          params: {
            addressId
          },
        });
      }
    },

  },
};
</script>

<style>
</style>
